{ "metadata": { "name": "", "signature": "sha256:c3e33dc11003c69beadcd211d1246a49287debf8cbc4b6561aa01c46e5ea31b1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The brythonmagic extension has been tested on:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import IPython\n", "IPython.version_info" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "brythonmagic installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just type the following:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%install_ext https://raw.github.com/kikocorreoso/brythonmagic/master/brythonmagic.py" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext brythonmagic" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And load the brython js lib in the notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%HTML\n", "" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Warning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to load javascript libraries in a safety way you should try to use https instead of http when possible (read more [here](http://mail.scipy.org/pipermail/ipython-dev/2014-July/014572.html)). If you don't trust the source and/or the source cannot be loaded using https then you could download the javascript library and load it from a local location." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Usage:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The brythonmagic provides you a cell magic, `%%brython`, to run brython code and show the results in a html `div` tag below the code cell.\n", "\n", "You can use several options:\n", "\n", "* -p, --print: will show you the generated html code below the results obtained from the brython code.\n", "\n", "\n", "* -c, --container: you can define de name of the `div` container in case you want to 'play' with it in other cell. If you don't define an output the `div` will have and `id` with the following format 'brython-container-[random number between 0 and 999999]'\n", "\n", "\n", "* -i, --input: you can pass variables defined in the Python namespace separated by commas. If you pass a python list it will be converted to a brython list, a python tuple will be converted to a brython tuple, a python dict will be converted to a brython dict, a python string will be converted to a brython string.\n", "\n", "\n", "* -h, --html: you can pass a string with html markup code. This html code will be inserted inside the div container. In this way you can avoid the generation of HTML markup code via a Brython script so you can separate the layout from the 'action'.\n", "\n", "\n", "* -s, --script: Use this option to provide and id to the script defined in the Brython code cell. Also, this value could be used to run the code of this cell in other brython cells.\n", "\n", "\n", "* -S, --scripts: Use this option to run code previously defined in other Brython code cells. The values should be the provided values in the -s/--script option in other Brython code cells.\n", "\n", "* -f, --fiddle: With this option, the code in the cell will be automatically uploaded to [gist.github.com/](https://gist.github.com/) as an anonymous gist with several files in it. This files will be used to create an anonymous 'fiddle' on [jsfiddle.net](http://jsfiddle.net). Finally, some links will be printed in the output linking to the gist and the fiddle. See an example here ([https://gist.github.com/anonymous/b664e8b4617afc09db6c](https://gist.github.com/anonymous/b664e8b4617afc09db6c) and [http://jsfiddle.net/gh/gist/library/pure/b664e8b4617afc09db6c/](http://jsfiddle.net/gh/gist/library/pure/b664e8b4617afc09db6c/))\n", "\n", "* -e, --embedfiddle: With this option, the code in the cell will be automatically uploaded to [gist.github.com/](https://gist.github.com/) as an anonymous gist with several files in it. This files will be used to create an anonymous 'fiddle' on [jsfiddle.net](http://jsfiddle.net). Finally, some links will be printed in the output linking to the gist and the fiddle and an iframe will be created showing the fiddle on [jsfiddle.net](http://jsfiddle.net).\n", "\n", "[WARNING] This options may change as the brythonmagic is in active development." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "`-p, --print` option" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following example shows the use of the `-p`, `--print` option. \n", "\n", "[HINT] The result of the print is shown in the javascript console of your browser." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%brython -p\n", "print('hello world!')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "`-c, --container` option" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following example can be seen the use of the `-c`, `--container`. The `-p` is also used to show you the result. See the `id` attribute of the `div` tag created:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%brython -c my_container -p\n", "from browser import doc, html\n", "\n", "# This will be printed in the js console of your browser\n", "print('Hello world!')\n", "\n", "# This will be printed in the container div on the output below\n", "doc[\"my_container\"] <= html.P(\"This text is inside the div\", \n", " style = {\"backgroundColor\": \"cyan\"})" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "`-i, --input` option" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example you can see how the data are passed to brython from python using the `-i` or `--input` option. First, we create some data in a regular Python cell." ] }, { "cell_type": "code", "collapsed": false, "input": [ "data_list = [1,2,3,4]\n", "\n", "data_tuple = (1,2,3,4)\n", "\n", "data_dict = {'one': 1, 'two': 2}\n", "\n", "data_str = \"\"\"\n", "Hello\n", "GoodBye\n", "\"\"\"\n", "\n", "# A numpy array can be converted to a list and you will obtain a brython list\n", "import numpy as np\n", "data_arr = np.empty((3,2))\n", "data_arr = data_arr.tolist()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now, the created data are passed to Brython and used in the Brython code cell. **Remember that only Python lists, tuples, dicts and strings are allowed as inputs**." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%brython -c p2b_data_example -i data_list data_tuple data_dict data_str data_arr\n", "from browser import doc, html\n", "\n", "doc[\"p2b_data_example\"] <= html.P(str(data_list))\n", "doc[\"p2b_data_example\"] <= html.P(str(type(data_list)))\n", "\n", "doc[\"p2b_data_example\"] <= html.P(str(data_tuple))\n", "doc[\"p2b_data_example\"] <= html.P(str(type(data_tuple)))\n", "\n", "doc[\"p2b_data_example\"] <= html.P(str(data_dict))\n", "doc[\"p2b_data_example\"] <= html.P(str(type(data_dict)))\n", "\n", "doc[\"p2b_data_example\"] <= html.P(data_str.replace('Hello', 'Hi'))\n", "doc[\"p2b_data_example\"] <= html.P(str(type(data_str)))\n", "\n", "doc[\"p2b_data_example\"] <= html.P(str(data_arr))\n", "doc[\"p2b_data_example\"] <= html.P(str(type(data_arr)))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "`-h, --html` option" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example you can see how to create some HTML code in a cell and then use that HTML code in the brython cell. In this way you do not need to create the HTML code via scripting with Brython." ] }, { "cell_type": "code", "collapsed": false, "input": [ "html = \"\"\"\n", "
` tag for each number.\n",
"\n",
"[HINT] To see the line numbers in the code cell just go to the cell and press **`
' not in cell.html:\n",
" cell.html += \"
cell #\" + str(i)\n",
" else:\n",
" if 'In' in cell.text:\n",
" cell.html = cell.html.split('
')[0]\n",
"\n",
"show_cell_number(on = True)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Running Python cells as a loop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Imagine you have several cells of code and you want just to modify some data and run again these cells as a loop not having to create a big cell with the code of the cells together."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%brython\n",
"from javascript import JSObject\n",
"from browser import window\n",
"\n",
"IPython = window.IPython\n",
"nb = IPython.notebook\n",
"# This is used to prevent an infinite loop\n",
"this_cell = nb.get_selected_index()\n",
"\n",
"for i in range(1,10): # Ths will run cells 1 to 9 (the beginning of the nb)\n",
" cell = nb.get_cell(i)\n",
" if cell.cell_type == \"code\" and i != this_cell:\n",
" cell.execute()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Get the code of all the cells and create a new cell with the code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to compile all the code used in a notebook you can use this recipe (use `crtl + Enter` to run the cell if you don't want a bad behaviour):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%brython\n",
"from javascript import JSObject\n",
"from browser import window\n",
"\n",
"IPython = window.IPython\n",
"nb = IPython.notebook\n",
"this_cell = nb.get_selected_index()\n",
"total_cells = nb.ncells()\n",
"\n",
"code = \"\"\n",
"first_cell = True\n",
"for i in range(total_cells):\n",
" cell = nb.get_cell(i)\n",
" if cell.cell_type == \"code\" and i != this_cell:\n",
" if first_cell:\n",
" code += \"# This cell has been generated automatically using a brython script\\n\\n\"\n",
" code += \"# code from cell \" + str(i) + '\\n'\n",
" first_cell = False\n",
" else:\n",
" code += \"\\n\\n\\n# code from cell \" + str(i) + '\\n'\n",
" code += cell.get_text() + '\\n'\n",
"\n",
"nb.insert_cell_below('code')\n",
"new_cell = nb.get_cell(this_cell + 1)\n",
"new_cell.set_text(code)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Styling the nb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets modify a little bit the look of the notebook. Warning: The result will be very ugly..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%brython -s styling\n",
"from browser import doc, html\n",
"\n",
"# Changing the background color\n",
"body = doc[html.BODY][0]\n",
"body.style = {\"backgroundColor\": \"#99EEFF\"}\n",
" \n",
"# Changing the color of the imput prompt\n",
"inps = body.get(selector = \".input_prompt\")\n",
"for inp in inps:\n",
" inp.style = {\"color\": \"blue\"}\n",
" \n",
"# Changin the color of the output cells\n",
"outs = body.get(selector = \".output_wrapper\")\n",
"for out in outs:\n",
" out.style = {\"backgroundColor\": \"#E0E0E0\"}\n",
" \n",
"# Changing the font of the text cells\n",
"text_cells = body.get(selector = \".text_cell\")\n",
"for cell in text_cells:\n",
" cell.style = {\"fontFamily\": \"\"\"\"Courier New\", Courier, monospace\"\"\",\n",
" \"fontSize\": \"20px\"}\n",
" \n",
"# Changing the color of the code cells.\n",
"code_cells = body.get(selector = \".CodeMirror\")\n",
"for cell in code_cells:\n",
" cell.style = {\"backgroundColor\": \"#D0D0D0\"}"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}